package com.bingxue.edu.tools;

import com.bingxue.edu.common.enums.EnrollmentStatusEnum;
import com.bingxue.edu.common.enums.TermEnum;
import com.bingxue.edu.common.util.EduUtil;
import com.bingxue.edu.management.education.model.entity.ClassInfo;
import com.bingxue.edu.management.education.model.entity.Enrollment;
import com.bingxue.edu.management.education.service.ClassInfoService;
import com.bingxue.edu.management.education.service.EnrollmentService;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

import static com.bingxue.edu.management.education.model.entity.table.ClassInfoTableDef.CLASS_INFO;
import static com.bingxue.edu.management.education.model.entity.table.EnrollmentTableDef.ENROLLMENT;

/**
 * 续报状态修复
 * @author panbo
 * @since 2025/2/15
 */
@SpringBootTest
public class RenewalStatusFixedTool {

    @Autowired
    private ClassInfoService classInfoService;
    @Autowired
    private EnrollmentService enrollmentService;

//    @Test
    public void fixed() {
        // 2025寒假班级
        QueryWrapper queryWrapper = QueryWrapper.create()
                .from(ENROLLMENT)
                .leftJoin(CLASS_INFO).on(CLASS_INFO.ID.eq(ENROLLMENT.CLASS_ID))
                .where(CLASS_INFO.CLASS_YEAR.eq(2025))
                .and(CLASS_INFO.TERM.eq(TermEnum.WINTER))
                .and(ENROLLMENT.CANCEL_LESSON_COUNT.lt(ENROLLMENT.ENROLL_LESSON_COUNT));
        List<Enrollment> enrollments = enrollmentService.getMapper().selectListByQuery(queryWrapper);
        for (Enrollment currentEnrollment : enrollments) {
            List<String> renewalClassIds = new ArrayList<>();
            // 非全退，存在课消
            if (currentEnrollment.getCancelLessonCount() < currentEnrollment.getEnrollLessonCount()) {
                ClassInfo currentClassInfo = classInfoService.getById(currentEnrollment.getClassId());
                // 查询后续班级报名记录，给当前班级设置续报班级为后续班级列表
                List<Enrollment> nextEnrollmentList = enrollmentService.queryChain()
                        .select(ENROLLMENT.ALL_COLUMNS)
                        .from(ENROLLMENT)
                        .leftJoin(CLASS_INFO).on(CLASS_INFO.ID.eq(ENROLLMENT.CLASS_ID))
                        .where(ENROLLMENT.STUDENT_ID.eq(currentEnrollment.getStudentId()))
                        .and(ENROLLMENT.CANCEL_LESSON_COUNT.lt(ENROLLMENT.ENROLL_LESSON_COUNT))
                        .and(CLASS_INFO.CLASS_YEAR.eq(EduUtil.getNextClassYear(currentClassInfo)))
                        .and(CLASS_INFO.TERM.eq(EduUtil.getNextClassTerm(currentClassInfo)))
                        .and(CLASS_INFO.SUBJECT.eq(currentClassInfo.getSubject()))
                        .list();
                for (Enrollment nextEnrollment : nextEnrollmentList) {
                    if(!renewalClassIds.contains(nextEnrollment.getClassId())) {
                        renewalClassIds.add(nextEnrollment.getClassId());
                    }
                }
            }
            Enrollment updateEnrollment = UpdateEntity.of(Enrollment.class, currentEnrollment.getId());
            updateEnrollment.setRenewalClassIds(renewalClassIds.isEmpty() ? null: renewalClassIds);
            enrollmentService.getMapper().update(updateEnrollment);
        }
    }

//    @Test
    public void test() {
        String classId = "2025Q4497086";
        long count = enrollmentService.queryChain()
                .where(ENROLLMENT.CLASS_ID.eq(classId))
                .and(ENROLLMENT.RENEWAL_CLASS_IDS.isNotNull())
                .and(ENROLLMENT.CANCEL_LESSON_COUNT.lt(ENROLLMENT.ENROLL_LESSON_COUNT))
                .count();
        System.out.println("留存人数: " + count);
//        String enrollmentId = "50555719241000135";
        String enrollmentId = "50569547548000149";
        Enrollment currentEnrollment = enrollmentService.getById(enrollmentId);
        System.out.println("currentEnrollment：" + currentEnrollment);
        ClassInfo currentClassInfo = classInfoService.getById(currentEnrollment.getClassId());
        // 查询后续班级报名记录，给当前班级设置续报班级为后续班级列表
        List<Enrollment> nextEnrollmentList = enrollmentService.queryChain()
                .select(ENROLLMENT.ALL_COLUMNS)
                .from(ENROLLMENT)
                .leftJoin(CLASS_INFO).on(CLASS_INFO.ID.eq(ENROLLMENT.CLASS_ID))
                .where(ENROLLMENT.STUDENT_ID.eq(currentEnrollment.getStudentId()))
                .and(ENROLLMENT.CANCEL_LESSON_COUNT.lt(ENROLLMENT.ENROLL_LESSON_COUNT))
                .and(CLASS_INFO.CLASS_YEAR.eq(EduUtil.getNextClassYear(currentClassInfo)))
                .and(CLASS_INFO.TERM.eq(EduUtil.getNextClassTerm(currentClassInfo)))
                .and(CLASS_INFO.SUBJECT.eq(currentClassInfo.getSubject()))
                .list();

        for (Enrollment nextEnrollment : nextEnrollmentList) {
            System.out.println("存在续报班级: " + nextEnrollment.getId());
        }
    }

//    @Test
    public void fixed2() {
        // 2025寒假班级
        QueryWrapper queryWrapper = QueryWrapper.create()
                .from(ENROLLMENT)
                .leftJoin(CLASS_INFO).on(CLASS_INFO.ID.eq(ENROLLMENT.CLASS_ID))
                .where(CLASS_INFO.CLASS_YEAR.eq(2025))
                .and(CLASS_INFO.TERM.eq(TermEnum.WINTER))
                .and(ENROLLMENT.CANCEL_LESSON_COUNT.eq(ENROLLMENT.ENROLL_LESSON_COUNT))
                .and(ENROLLMENT.STATUS.eq(EnrollmentStatusEnum.CANCELED))
                ;
        List<Enrollment> enrollments = enrollmentService.getMapper().selectListByQuery(queryWrapper);
        for (Enrollment currentEnrollment : enrollments) {
            // 取消
            Enrollment updateEnrollment = UpdateEntity.of(Enrollment.class, currentEnrollment.getId());
            updateEnrollment.setRenewalClassIds(null);
            enrollmentService.getMapper().update(updateEnrollment);
//            enrollmentService.updateById(currentEnrollment);
        }
    }
}
